package com.funny.stock.user.util;

import com.alibaba.fastjson.JSON;
import com.funny.stock.base.vo.Result;
import com.funny.stock.user.vo.SysUserVO;

import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ch.qos.logback.core.util.CloseUtil;

/**
 * @author pengl
 * @date 2020/05/17
 **/
public class UserSecurityUtil {
    public static final String ATTR_USER = "user";

    public static final Integer SESSION_VALID_TIME_SECOND = 86400;

    public static final String REQUEST_CODE = "code";
    public static final Integer REQUEST_CODE_SUCCESS = 0;
    public static final Integer REQUEST_CODE_FAIL = -1;
    public static final String REQUEST_MSG = "message";

    private UserSecurityUtil(){}

    public static SysUserVO getUser(){
        return (SysUserVO) getSessionAttribute(ATTR_USER);
    }

    public static Long getUserId(){
        SysUserVO userVO = (SysUserVO) getSessionAttribute(ATTR_USER);
        return userVO == null ? null : userVO.getUserId();
    }

    public static void setUser(SysUserVO sysUserVO){
        setSessionAttribute(ATTR_USER, sysUserVO);
    }

    public static void removeUser(){
        HttpServletRequest request = getHttpServletRequest();
        if(request == null) return;

        HttpSession session = request.getSession();
        if(session != null)
            session.removeAttribute(ATTR_USER);
    }

    public static HttpServletRequest getHttpServletRequest() {
        if(RequestContextHolder.getRequestAttributes() == null) return null;

        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpSession getHttpSession() {
        HttpServletRequest request = getHttpServletRequest();
        if(request == null) return null;

        return request.getSession();
    }


    public static void removeSessionAttribute(String name){
        RequestContextHolder.getRequestAttributes().removeAttribute(name, RequestAttributes.SCOPE_SESSION);
    }


    public static void setRequestAttribute(String name,Object value){
        RequestContextHolder.getRequestAttributes().setAttribute(name, value, RequestAttributes.SCOPE_REQUEST);
    }

    public static Object getRequestAttribute(String name){
        return RequestContextHolder.getRequestAttributes().getAttribute(name, RequestAttributes.SCOPE_REQUEST);
    }

    public static void setSessionAttribute(String name,Object value){
        RequestContextHolder.getRequestAttributes().setAttribute(name, value, RequestAttributes.SCOPE_SESSION);
    }

    public static Object getSessionAttribute(String name){
        return RequestContextHolder.getRequestAttributes().getAttribute(name, RequestAttributes.SCOPE_SESSION);
    }

    public static void setRequestFailMsg(String message){
        setRequestAttribute(REQUEST_CODE, REQUEST_CODE_FAIL);
        setRequestAttribute(REQUEST_MSG, message);
    }

    public static String getRequestFailMsg(){
        Integer reqCode = (Integer) getRequestAttribute(REQUEST_CODE);
        if(reqCode == null) return null;

        return (String) getRequestAttribute(REQUEST_MSG);
    }



    public static void writeResponseResult(HttpServletResponse response, Result data)  {
        String json = JSON.toJSONString(data);
        writeResponseJsonText(response, HttpStatus.OK.value(), json);
    }

    public static void writeResponseJsonText(HttpServletResponse response,int status,String json)  {
        response.setStatus(status);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(json);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            CloseUtil.closeQuietly(out);
        }
    }
}
